package com.zyz.bookborrowing.servlet;

import com.zyz.bookborrowing.bean.*;
import com.zyz.bookborrowing.service.*;
import com.zyz.bookborrowing.utils.BookFlagE;
import com.zyz.bookborrowing.utils.Constant;
import com.zyz.bookborrowing.utils.StringUtils;
import org.apache.taglibs.standard.lang.jstl.DivideOperator;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@WebServlet(name = "readerCardServlet", urlPatterns = "/readerCardServlet")
public class ReaderCardServlet extends HttpServlet {

    IReaderCardService readerCardService = new ReaderCardServiceImpl();
    IStudentService studentService = new StudentServiceImpl();
    IBookService bookService = new BookServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1. 获取请求的类型
        String type = req.getParameter(Constant.REQUEST_PARAMETER_TYPE);
//        2. 如果type不为空，那么根据type不同值做出不同操作
        if (StringUtils.isNotEmpty(type)){
            if (Constant.SERVLET_TYPE_QUERY.equals(type)){
                queryList(req, resp);
            } else if (Constant.SERVLET_TYPE_SAVE.equals(type)){
                saveCard(req,resp);
            } else if (Constant.SERVLET_TYPE_UPDATE.equals(type)) {
            } else if (Constant.SERVLET_TYPE_DELETE.equals(type)) {
            } else if (Constant.SERVLET_TYPE_QUERYBYID.equals(type)) {
            } else if ("saveBefore".equals(type)) {
//                获取所有的学生信息
                List<Student> list = studentService.list(null);
                req.setAttribute("stus", list);
                req.getRequestDispatcher("/card/cardAdd.jsp").forward(req, resp);
            } else if ("queryBooks".equals(type)) {
//                查询所有可以借阅的书籍
                String cardId = req.getParameter("id");
                List<Book> list = bookService.queryListByState(BookFlagE.FREE.code);
                req.setAttribute("books", list);
                req.setAttribute("cardId", cardId);
                req.getRequestDispatcher("/card/bookBorrow.jsp").forward(req, resp);
            } else {
            }
        } else {
        }
    }

    private void queryList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<ReaderCard> list = null;
//                查询当前登录用户对应的学生的学生卡信息
        User user = (User) req.getSession().getAttribute(Constant.SERVLET_LOGIN_USER);
        if (!"admin".equals(user.getUsername())){
//                    如果不是管理员 就不能查询所有的卡数据
//                    根据登录用户找学生信息
            String stuNum = user.getUsername();
            Student stu = studentService.queryByStuNum(stuNum);
//                    根据学生编号查询借书卡情况
            if (stu != null){
                list = readerCardService.queryByStuId(stu.getStuNum());
            }
        } else {
            list =readerCardService.list(null);
        }

        req.setAttribute("list",list);
        req.getRequestDispatcher("/card/card.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    private void saveCard(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ReaderCard card = new ReaderCard();
        String id = req.getParameter("stuId");
        Student student = studentService.queryById(Integer.parseInt(id));
        card.setStuName(student.getStuName());
        card.setStuId(student.getStuNum());
//        随机生成密码
        String password = UUID.randomUUID().toString();
        card.setPassword(password);

        Integer count = -1;
//        if (id == null || id.equals("")){
//                count:影响的行数
            count = readerCardService.saveReaderCard(card);
//            System.out.println("添加");
//        } else {
//            System.out.println("更新");
//            card.setId(Integer.parseInt(id));
////            更新
//            count = readerCardService.updateReaderCard(card);
//        }
        if (count > 0){
//                    表示增/更成功,再做一次查询操作
            resp.sendRedirect("/readerCardServlet?type=query");
        } else {
//                    插入失败
            System.out.println("插入失败...");
//                    跳转失败页面
        }
    }
}
